package core.map
{
	import core.map.dataobject.MapLayer;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.utils.Dictionary;

	public class LayerManager
	{
		
		
		private var _container:DisplayObjectContainer;
		
		private var _layerChildNum:Array;
		private var _layerIndex:Array;
		
		public function LayerManager()
		{
			
			_layerChildNum = [];
			_layerIndex = [];
			_layerIndex[MapLayer.BACK_GROUND] = 0;
			_layerIndex[MapLayer.GROUND] = 1;
			_layerIndex[MapLayer.MAIN] = 2;
			_layerIndex[MapLayer.MAIN2] = 3;
			_layerIndex[MapLayer.SCENE_EFFECT] = 4;
			for(var i:int = 0;i<5;i++)
			{
				_layerChildNum[i] = 0;
			}
			
			
		}
		
		public function init(container:DisplayObjectContainer):void
		{
			_container = container;
		}
		
		public function numChildren(layer:String):int
		{
			if(_layerIndex[layer]!=null)
				return _layerChildNum[_layerIndex[layer]];
			else
				return -1;
		}
		
		public function addChild(child:DisplayObject,layer:String):void
		{
			var addIndex:int = getPrevLayersCount(layer)
			if(addIndex == -1)
			{
				throw new Error("TGP:Do not have layer named "+layer+".Add child failed");
			}
			addIndex += _layerChildNum[_layerIndex[layer]];
			_container.addChildAt(child,addIndex);
			
			_layerChildNum[_layerIndex[layer]]++;
		}
		
		public function addChildAt(child:DisplayObject,index:int,layer:String):void
		{
			var prevLayerChildNum:int = getPrevLayersCount(layer);
			if(prevLayerChildNum == -1)
				throw new Error("TGP:Do not have layer named "+layer+".Add child failed");
			if(index>_layerChildNum[_layerIndex[layer]]-1)
				throw new Error("TGP:index exceed layer numChildren");
			var addIndex:int = prevLayerChildNum+index;
			
			_container.addChildAt(child,addIndex);
			
			_layerChildNum[_layerIndex[layer]]++;
		}
		
		public function removeChild(child:DisplayObject,layer:String):void
		{
			if(_container.contains(child))
			{
				_container.removeChild(child);
				_layerChildNum[_layerIndex[layer]]--;
			}
		}
		
		public function removeChildAt(index:int,layer:String):void
		{
			throw new Error("TGP:removeChildAt method not implemented");
		}
		
		public function getChildAt(index:int,layer:String):DisplayObject
		{
			var prevLayerChildNum:int = getPrevLayersCount(layer);
			if(prevLayerChildNum == -1)
				throw new Error("TGP:Do not have layer named "+layer+".Add child failed");
			if(index>_layerChildNum[_layerIndex[layer]]-1)
				throw new Error("TGP:index exceed layer numChildren");
			var actualIndex:int = prevLayerChildNum+index;
			return _container.getChildAt(actualIndex);
		}

		
		private function getPrevLayersCount(layer:String):int
		{
			if(_layerIndex[layer]== null) return -1;
			
			var layerIndex:int = _layerIndex[layer];
			var index:int = 0;
			for(var i:int = 0;i<layerIndex;i++)
			{
				index += _layerChildNum[i];
			}
			return index;
		}
	}
}